package com.example.administrator.swipemenulistviewtest;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2015/4/2.
 */
class AppAdapter extends BaseAdapter {
    private List<ApplicationInfo> mAppList;
    private Context mContext;

    AppAdapter(List<ApplicationInfo> mAppList, Context mContext) {
        this.mAppList = mAppList;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mAppList.size();
    }

    @Override
    public ApplicationInfo getItem(int position) {
        return mAppList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext.getApplicationContext(),
                    R.layout.item_list_app, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();

        }
        ApplicationInfo item = getItem(position);
        viewHolder.ivIcon.setImageDrawable(item.loadIcon(mContext.getPackageManager()));
        viewHolder.tvName.setText(item.loadLabel(mContext.getPackageManager()));
        return convertView;
    }

    class ViewHolder {
        @InjectView(R.id.iv_icon)
        ImageView ivIcon;
        @InjectView(R.id.tv_name)
        TextView tvName;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
